﻿@using SimpleIdServer.IdServer.Api.AuthenticationSchemeProviders;
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.IdProviderStore;
@using System.Reflection;
@inject IState<UpdateIdProviderState> updateIdProviderState

<RadzenTemplateForm Submit=@SubmitUpdateUserPropertyIdProvider TItem="UserPropertyIdProviderMapperForm" Data=@userPropertyIdProviderMapperForm>
    <!-- Name -->
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.Name</RadzenText>
        <RadzenTextBox Name="Name" @bind-Value="@userPropertyIdProviderMapperForm.Name" Class="w-100"></RadzenTextBox>
        <RadzenRequiredValidator Component="Name" Text="@Global.NameIsRequired"></RadzenRequiredValidator>
    </div>
    <!-- Token Claim Name -->
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.SourceClaimName</RadzenText>
        <RadzenTextBox Name="SourceClaimName" @bind-Value="@userPropertyIdProviderMapperForm.SourceClaimName" Class="w-100"></RadzenTextBox>
        <RadzenRequiredValidator Component="SourceClaimName" Text="@Global.SourceClaimNameRequired"></RadzenRequiredValidator>
    </div>
    <!-- Property -->
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.UserProperty</RadzenText>
        <RadzenDropDown Name="UserPropertyName" Class="w-100"
                        Data=@userProperties
                        TValue="String"
                        @bind-Value=@userPropertyIdProviderMapperForm.TargetUserProperty />
        <p class="text-muted">@Global.PropertyName</p>
        <RadzenRequiredValidator Component="UserPropertyName" Text="@Global.UserPropertyRequired"></RadzenRequiredValidator>
    </div>
    <RadzenButton class="mt-1" Variant="Variant.Flat" ButtonType="ButtonType.Submit" ButtonStyle="ButtonStyle.Success" Text="@(updateIdProviderState.Value.IsUpdating ? Global.Saving : Global.Save)" Disabled="@(updateIdProviderState.Value.IsUpdating)" />
</RadzenTemplateForm>

@code {
    [Parameter]
    public EventCallback<AuthenticationSchemeProviderMapperResult> AuthProviderMapperUpdated { get; set; }
    [Parameter]
    public AuthenticationSchemeProviderMapperResult Mapper { get; set; }
    IEnumerable<string> userProperties = new List<string>();

    record UserPropertyIdProviderMapperForm
    {
        public string Name { get; set; }
        public string SourceClaimName { get; set; }
        public string? TargetUserProperty { get; set; } = null;
    }

    UserPropertyIdProviderMapperForm userPropertyIdProviderMapperForm = new UserPropertyIdProviderMapperForm();

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        userProperties = typeof(User).GetProperties(BindingFlags.Instance | BindingFlags.Public)
                .Where(p =>
                {
                    var attr = p.GetCustomAttribute<UserPropertyAttribute>();
                    return attr == null ? false : attr.IsVisible;
                })
                .Select(p => p.Name);
        if (Mapper != null)
        {
            userPropertyIdProviderMapperForm.Name = Mapper.Name;
            userPropertyIdProviderMapperForm.SourceClaimName = Mapper.SourceClaimName;
            userPropertyIdProviderMapperForm.TargetUserProperty = Mapper.TargetUserProperty;
        }
    }

    async void SubmitUpdateUserPropertyIdProvider(UserPropertyIdProviderMapperForm value)
    {
        await AuthProviderMapperUpdated.InvokeAsync(new AuthenticationSchemeProviderMapperResult
        {
            Name = value.Name,
            SourceClaimName = value.SourceClaimName,
            TargetUserProperty = value.TargetUserProperty,
            MapperType = MappingRuleTypes.USERPROPERTY
        });
    }
}